package com.gz.tank;
 import java.awt.*;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
 import java.util.Vector;
 import javax.swing.*;
 import java.io.*;

/**
 * @gz
 */
public class Mypanel extends JPanel implements KeyListener,Runnable{
    Vector <Bomb>bombs=new Vector<>();
    Vector<Node> nodes=new Vector<>();
    Image image1=null;//定义三种炸弹爆炸效果
    Image image2=null;
    Image image3=null;
    Hero hero =null;
    Vector<EnemyTank>enemyTanks= new Vector<>();
    int enemyTanksSize=3;//设置坦克数量
    public Mypanel(String key){
        File file=new File(Recorder.getRecordfilepath());
        if(file.exists()){
            nodes=Recorder.getNodesenemy();
        }
        else{
            System.out.println("文件不存在，开始新游戏");
            key="0";
        }
        Recorder.setEnemyTanks(enemyTanks);//后面paint每次刷新自动会改变enemyTanks数组的内容,用于存取
        hero =new Hero(700,400);//地图1000*750
        hero.setSpeed(5);
        switch (key){
            case "1"://继续游戏
                for(int i=0;i<nodes.size();i++){//注:与下面不一样，这里enemyTanksSize不能换，因为初始状态敌人坦克数为0，而enemyTanksSize是我自己设的
                    Node node=nodes.get(i);
                    EnemyTank enemyTank=new EnemyTank(node.getX(),node.getY());
                    enemyTank.setEnemies(enemyTanks);//用来防止重叠,防碰撞
                    enemyTank.setDirect(node.getDirect());//设置敌人坦克方向
                    new Thread(enemyTank).start();//敌人坦克开始移动

                    //这里没有写子弹的存取和保存，我可以实现但没写(写了又删)，这里敌人只有每次发射一发子弹，可以保存，
                    //但若敌人多发子弹，要看子弹数组大小，还没想出来如何存取

                    Shot shot =new Shot(enemyTank.getX()+20,enemyTank.getY()+60,enemyTank.getDirect());//给每个敌人坦克增加子弹
                    enemyTank.shots.add(shot);
                    new Thread(shot).start();//启动shot对象
                    enemyTanks.add(enemyTank);
                }
                image1=Toolkit.getDefaultToolkit().getImage(getClass().getResource("/picture(1).gif"));
                image2=Toolkit.getDefaultToolkit().getImage(getClass().getResource("/picture(2).gif"));
                image3=Toolkit.getDefaultToolkit().getImage(getClass().getResource("/picture(3).gif"));
                break;
            case "0"://重新开始
                //游戏初始化
                Recorder.setEnemyNum(0);
                for(int i=0;i<enemyTanksSize;i++){//注:与下面不一样，这里enemyTanksSize不能换，因为初始状态敌人坦克数为0，而enemyTanksSize是我自己设的
                    EnemyTank enemyTank=new EnemyTank(100*(i+1),0);
                    enemyTank.setEnemies(enemyTanks);//用来防止重叠,防碰撞
                    enemyTank.setDirect(2);//设置敌人坦克方向
                    new Thread(enemyTank).start();//敌人坦克开始移动
                    Shot shot =new Shot(enemyTank.getX()+20,enemyTank.getY()+60,enemyTank.getDirect());//给每个敌人坦克增加子弹
                    enemyTank.shots.add(shot);
                    new Thread(shot).start();//启动shot对象
                    enemyTanks.add(enemyTank);
                }
                image1=Toolkit.getDefaultToolkit().getImage(getClass().getResource("/picture(1).gif"));
                image2=Toolkit.getDefaultToolkit().getImage(getClass().getResource("/picture(2).gif"));
                image3=Toolkit.getDefaultToolkit().getImage(getClass().getResource("/picture(3).gif"));
                break;
        }
        new AePlayWave("src\\111.wav").start();
    }

    //计分板
    public void showscores(Graphics g){
        //画出总成绩(字体黑色)
        g.setColor(Color.black);
        Font font = new Font("宋体", Font.BOLD, 25);
        g.setFont(font);

        g.drawString("您累计击毁敌方坦克:",1020,30);
        drawTank(1020,60,g,0,1);
        g.setColor(Color.black);
        g.drawString(Recorder.getEnemyNum()+"",1080,100);
    }

//    public String trans_int_str(int i){
//        return String.valueOf(i);
//    }

    public void paint(Graphics g){
        super.paint(g);
        //填充矩形，用做背景，默认黑色
        g.fillRect(0,0,1000,750);
        showscores(g);
        if(hero!=null && hero.islive) {
            drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 0);
        }

        for (int i = 0; i < hero.shots.size(); i++) {
            Shot shot = hero.shots.get(i);
            if (shot != null && shot.islive == true) {//画出我们的子弹
                g.draw3DRect(shot.getX(), shot.getY(), 1, 1, false);
            } else
                hero.shots.remove(shot);
        }

        //如果bombs集合有对象，就画出
        for (int i = 0; i < bombs.size(); i++) {
            //取出炸弹
            Bomb bomb=bombs.get(i);
            //根据当前bomb对象life值来实现炸弹的爆炸效果
            if(bomb.life>6)
                g.drawImage(image1,bomb.x,bomb.y,60,60,this);
            else if (bomb.life>3) {
                g.drawImage(image2,bomb.x,bomb.y,60,60,this);
            }
            else{
                g.drawImage(image3,bomb.x,bomb.y,60,60,this);
            }
            bomb.lifedown();
            if(bomb.life==0){
                bombs.remove(bomb);
            }
        }


        //左上角x,y,方向,颜色
/*       drawTank(hero.getX()+100, hero.getY(),g,1,1);
        drawTank(hero.getX()+200, hero.getY(),g,2,1);
        drawTank(hero.getX()+300, hero.getY(),g,3,1);*/
        for (int i = 0; i <enemyTanks.size() ; i++) {//注意！！，这里enemyTanks.size()容易写成enemyTanksSize,导致无法重新获取数组大小，造成越界，下面的也一样，最好用.size()
            EnemyTank enemyTank=enemyTanks.get(i);
            if(enemyTank.islive){
                drawTank(enemyTank.getX(),enemyTank.getY(),g,enemyTank.getDirect(),1);
                for (int j = 0; j < enemyTank.shots.size(); j++) {
                    Shot shot=enemyTank.shots.get(j);
                    if(shot.islive==true){
                        g.draw3DRect(shot.getX(),shot.getY(),1,1,false);//画出敌人子弹
                        // System.out.println("敌人子弹在动");
                    }else {
                        enemyTank.shots.remove(shot);
                    }//子弹到达边界时清除一颗子弹，否则一直留在Vector中
                 }
            }
        /*    else
                enemyTanks.remove(enemyTank);remove写在这里或者hit方法中都可以*/
        }
    }

    //绘制坦克方法
    public void drawTank(int x,int y,Graphics g,int direct,int colortype){
        switch(colortype){
            //坦克颜色
            case 0://我们的坦克
                g.setColor(Color.cyan);
                break;
            case 1://敌人的坦克
                g.setColor(Color.yellow);
        }
        //根据方向，绘制坦克
        //(顺时针)0表示向上,1表示向右,2,表示向下,3表示向左
        switch (direct){
            case 0://向上
                g.fill3DRect(x,y,10,60,false);
                g.fill3DRect(x+30,y,10,60,false);
                g.fill3DRect(x+10,y+10,20,40,false);
                g.fillOval(x+10,y+20,20,20);
                g.drawLine(x+20,y+30,x+20,y);
                break;
            case 1://向右
                g.fill3DRect(x,y,60,10,false);
                g.fill3DRect(x,y+30,60,10,false);
                g.fill3DRect(x+10,y+10,40,20,false);
                g.fillOval(x+20,y+10,20,20);
                g.drawLine(x+30,y+20,x+60,y+20);
                break;
            case 2://向下
                g.fill3DRect(x,y,10,60,false);
                g.fill3DRect(x+30,y,10,60,false);
                g.fill3DRect(x+10,y+10,20,40,false);
                g.fillOval(x+10,y+20,20,20);
                g.drawLine(x+20,y+30,x+20,y+60);
                break;
            case 3://向左
                g.fill3DRect(x,y,60,10,false);
                g.fill3DRect(x,y+30,60,10,false);
                g.fill3DRect(x+10,y+10,40,20,false);
                g.fillOval(x+20,y+10,20,20);
                g.drawLine(x+30,y+20,x,y+20);
                break;
            default:
                break;
        }

    }
    public void hit(Shot shot,Tank tank){
        if(hero.islive){
            switch(tank.getDirect()){
                //case 0 1 2 3根据敌人坦克朝向来判断敌人或自己坦克的坐标
                case 0://坦克朝上
                case 2://坦克朝下
                    if(shot.getX()>tank.getX()&&shot.getX()<tank.getX()+40
                            &&shot.getY()>tank.getY()&&shot.getY()<tank.getY()+60){
                        shot.islive=false;
                        tank.setIslive(false);
//                        hero.shots.remove(shot);//上面有这一步了
                        if(tank instanceof EnemyTank){
                            enemyTanks.remove(tank);//当我的子弹击中敌人坦克时，去掉敌人坦克，否则击中不会消失
                            Recorder.addenemyNum();
                        }
                        Bomb bomb=new Bomb(tank.getX(),tank.getY());
                        bombs.add(bomb);
                    }
                    break;
                case 1://敌人坦克朝右
                case 3://敌人坦克朝左
                    if(shot.getX()>tank.getX()&&shot.getX()<tank.getX()+60
                            &&shot.getY()>tank.getY()&&shot.getY()<tank.getY()+40){
                        shot.islive=false;
                        tank.setIslive(false);
                        if(tank instanceof EnemyTank){
                            enemyTanks.remove(tank);//当我的子弹击中敌人坦克时，去掉敌人坦克，否则击中不会消失
                            Recorder.addenemyNum();
                        }
//                        hero.shots.remove(shot);
                        Bomb bomb=new Bomb(tank.getX(),tank.getY());
                        bombs.add(bomb);
                    }
                    break;
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

        if(hero!=null&&hero.islive){
            if(e.getKeyCode()==KeyEvent.VK_LEFT){
                hero.setDirect(3);//向左转
                hero.moveleft();
            }
            else if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
                hero.setDirect(1);//向右转
                hero.moveright();
            }
            else if(e.getKeyCode()==KeyEvent.VK_UP){
                hero.setDirect(0);//向上转
                hero.moveup();
            }
            else if(e.getKeyCode()==KeyEvent.VK_DOWN){
                hero.setDirect(2);//向下转
                hero.movedown();
            }

            if(e.getKeyCode()==KeyEvent.VK_J){
                System.out.println("射击代码触发");
                if (hero.islive)
                    hero.myshot();//每按一次，子弹都从开始处重新出现
            }

            this.repaint();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    public void shotenemytankes()
    {
        //检测我方多颗子弹碰撞敌人坦克
            for (int i = 0; i < hero.shots.size(); i++) {
                Shot shot = hero.shots.get(i);
                if (shot != null && shot.islive == true) {
                    for (int j = 0; j < enemyTanks.size(); j++) {
                        EnemyTank enemyTank = enemyTanks.get(j);
                        hit(shot, enemyTank);
                    }
                }
            }
            //检测我方是否被敌人子弹打中
            for (int i = 0; i < enemyTanks.size(); i++) {
                EnemyTank enemyTank=enemyTanks.get(i);
                if(enemyTank!=null && enemyTank.islive == true){
                    for(int j=0;j<enemyTank.shots.size();j++){
                        Shot shot=enemyTank.shots.get(j);
                        hit(shot,hero);
                    }
                }
            }
    }

    @Override
    public void run(){
        while(true){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //每次重绘遍历一次子弹是否击中敌人坦克
            shotenemytankes();
            this.repaint();//让子弹不停重绘
        }
    }
}
